package com.itheima.health.service.impl;

import com.aliyun.oss.common.comm.ServiceClient;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.OrderSettingQD;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OrderSettingServicelmpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 通过月份获取预约
     * @param year
     * @param month
     * @return
     */
    @Override
    public List<OrderSettingQD> getOrderSettingByMonth(Integer year, Integer month) {
        //定义开始时间到结束时间
        LocalDate begin = LocalDate.of(year, month ,1);
        LocalDate end = begin.with(TemporalAdjusters.lastDayOfMonth());

        List<OrderSetting> orderSettings = orderSettingDao.getByMonthYear(begin, end);
        List<OrderSettingQD> orderSettingQDS = new ArrayList<>();

        for (OrderSetting orderSetting : orderSettings) {
            OrderSettingQD orderSettingQD = new OrderSettingQD();

             Date date = orderSetting.getOrderDate();
             int day = date.getDate();
             orderSettingQD.setDate(day);
             orderSettingQD.setNumber(orderSetting.getNumber());
             orderSettingQD.setReservations(orderSetting.getReservations());

             orderSettingQDS.add(orderSettingQD);
        }
        return orderSettingQDS;
    }

    /**
     * 通过 "日期" 编辑每天可以预约人数
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Integer number = orderSetting.getNumber();
        Date orderDate = orderSetting.getOrderDate();

        orderSettingDao.editNumberByDate(number,orderDate);

    }
}
